/*
	Copyright 2008 Jay Elliott

	This file is part of libemath.

	libemath is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	libemath is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with libemath.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "cMatrix.h"


emath::cMatrix emath::cMatrix::operator*(cMatrix other)
{
	cMatrix ret;

	float *res = ret.mat;
	for (int i = 0; i < 4; i++)
		for (int j = 0; j < 4; j++)
			res[j * 4 + i] = mat[0 + i] * other.mat[4 * j] + mat[4 + i] * other.mat[4 * j +1] + mat[8 + i] * other.mat[4 * j + 2] + mat[12 + i] * other.mat[4 * j + 3];


	return ret;
}


void emath::cMatrix::LoadIdentity(void)
{
	for (int i = 0; i < 16; i++)
		mat[i] = 0.0f;

	mat[0] = 1.0f;
	mat[5] = 1.0f;
	mat[10] = 1.0f;
	mat[15] = 1.0f;
}

emath::cPoint3d emath::operator*(cPoint3d point, cMatrix mat)
{
	cPoint3d ret;


	for (int i = 0; i < 4; i++)
	{
		float buffer = 0.0f;
		for (int j = 0; j < 4; j++)
			buffer += mat(j, i) * point[j];

		ret[i] = buffer;
	}

	return ret;
}

emath::cMatrix emath::cMatrix::transpose(void)
{
	cMatrix ret;

	for (int row = 0; row < 4; row++)
		for (int col = 0; col < 4; col++)
			ret(col, row) = (*this)(row, col);

	return ret;
}

bool emath::cMatrix::operator==(const cMatrix& other) const
{
	for (int i = 0; i < 16; i++)
		if (!equals(mat[i], other.mat[i]))
			return false;
	return true;
}
